package com.compass.ui.client;

import java.util.Arrays;

import com.compass.ui.client.data.TestData;
import com.compass.ui.client.model.Folder;
import com.compass.ui.client.model.Section;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;

public class ProjectTree extends ContentPanel
{
	TreeStore<ModelData> store;
	final TreeGrid<ModelData> tree;
	
	public ProjectTree()
	{
		super();
		setHeaderVisible(false);
		store = new TreeStore<ModelData>();
		store.add(TestData.getTreeModel().getChildren(), true);
		
		ColumnConfig name = new ColumnConfig("name", "Nom", 100);
	    name.setRenderer(new TreeGridCellRenderer<ModelData>()); 
	    ColumnConfig date = new ColumnConfig("author", "Date", 100);  
	    ColumnConfig author = new ColumnConfig("genre", "Auteur", 100);
	    author.setRenderer(new GridCellRenderer<ModelData>() {
			@Override
			public Object render(ModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<ModelData> store, Grid<ModelData> grid)
			{
				return (model.get(property) == null ? "" : new LinkLabel((String) model.get(property)));
			}
		});
	    ColumnModel cm = new ColumnModel(Arrays.asList(name, date, author));
	    tree = new TreeGrid<ModelData>(store, cm);
	    tree.setHeight("418px");
	    tree.setWidth("550px");
	    tree.setAutoExpandColumn("name");  
	    tree.setTrackMouseOver(false);  
	    tree.getAriaSupport().setLabelledBy(getHeader().getId() + "-label");
	    add(tree);
	}
	
	public void	addTreeListener(EventType eventType, Listener<? extends BaseEvent> listener)
	{
		tree.addListener(eventType, listener);
	}
}
